package cn.mily.pet.controller;

import cn.mily.basics.utils.JsonResult;
import cn.mily.basics.utils.PageBean;
import cn.mily.pet.domain.Pet;
import cn.mily.pet.domain.PetType;
import cn.mily.pet.domain.SearchMasterMsg;
import cn.mily.pet.domain.SourceType;
import cn.mily.pet.query.FindMastQuery;
import cn.mily.pet.service.IPetService;
import cn.mily.pet.service.ISearchService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private ISearchService searchMasterMsgService;

    @Autowired
    private IPetService service;

    @GetMapping("/{id}")
    public Pet findOne(@PathVariable("id") Long id){
        try {
            return service.findOne(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有宠物来源
     * @return
     */
    @PostMapping("/source")
    public List<SourceType> getSource(){
        try {
            return service.findAllSource();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有的宠物类型
     * @return
     */
    @PostMapping("/type")
    public List<PetType> getType(){
        try {
            return service.findAllType();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PatchMapping("/on")/*批量上架*/
    public JsonResult pachOn(@RequestBody Long[] ids){/*根据id数组批量上架*/
        try {
            service.patchOn(ids);
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("删除失败！");
        }
    }

    @PatchMapping("/off")/*批量下架*/
    public JsonResult pachOff(@RequestBody Long[] ids){/*根据id数组批量下架*/
        try {
            service.patchOff(ids);
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("删除失败！");
        }
    }

    @PatchMapping/*批量删除*/
    public JsonResult pachDelete(@RequestBody Long[] ids){/*根据id数组批量删除*/
        try {
            service.patchDelete(ids);
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("删除失败！");
        }
    }


    @DeleteMapping("/{id}")/*删除*/
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);/*根据id删除*/
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("添加失败！");
        }
    }

    /**
     * 宠物管理   列表展示
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pets")
    public PageBean<Pet> getPets(@RequestBody FindMastQuery query,HttpServletRequest request){
        try {
            return service.findAll(query,request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 处理寻主信息
     * @return
     */
    @PutMapping
    public JsonResult mission(@RequestBody Pet pet,HttpServletRequest request){
        try {
            if(pet.getId()==null){
                pet.setState(0);
                service.add(pet,request);
            }else{
                service.update(pet);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new JsonResult(e.getMessage());
        }
    }

    /**
     * 列表展示 + 查询
     * @param query
     * @return
     */
    @PostMapping
    public PageBean<SearchMasterMsg> getsearchMasterMsgService(@RequestBody FindMastQuery query,HttpServletRequest request){
        try {
             return searchMasterMsgService.findAll(query,request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
/*===========================================================================================*/
    /**
     * 添加宠物寻主信息
     * @return
     */
    @PostMapping("/searchMasterMsg")
    public JsonResult searchMasterMsg(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.searchMasterMsg(searchMasterMsg,request);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
    }


}
